package cn.itcast.usermanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@RequestMapping("/rest/user")
@Controller
public class NewUserController {
	@Autowired
	private UserService userService;
	/**
	 * 根据id查询用户信息
	 */
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	public ResponseEntity<User> queryUserById(@PathVariable(value="id")Long id){
		try {
			if(id<1){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			User user = this.userService.queryUserById(id);
			if(null==user){
				//404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
//			return ResponseEntity.status(HttpStatus.OK).body(user);
			return ResponseEntity.ok(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 新增用户数据
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> saveUser(User user){
		try {
			if(user==null || StringUtils.isEmpty(user)){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.saveUser(user);
//			//201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 修改数据
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateUser(User user){
		try {
			if(user.getId()==null || user.getId()<1){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.updateUser(user);
//			//204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 删除用户数据
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteUser(@RequestParam("ids") List<Object> ids){
		try {
			if(ids==null){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			boolean b = this.userService.deleteUser(ids);
			if(b){
				//204
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}else{
				//404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
